app.register.controller('DecorationCtrl', function($scope, $http, $rootScope,
		$route, DecorationSer, AppUtil, $q,$routeParams) {
	
	loadFlag($routeParams.id,$scope);
	//获取本地存储中的页码页数
	var page = AppUtil.GetLocalValue("decorationTable_Page");
	var size = AppUtil.GetLocalValue("decorationTable_Size");
	$scope.pagination = {
			totalItems :0,
			currentPage : page == undefined ? 1 : page,
			itemsPerPage : size == undefined ? 10 : size
	};
	
	
	$scope.showinfo={
			count1:22,
			count2:2,
			count3:5,
			count4:4,
			count5:4,
			count6:7,
			count7:7,
			count8:7
	};
	
	$scope.setCols=function(value,flag){
		var index = parseInt(value);
		switch(index){
		case 1:
			$scope.showinfo.count1+=flag?1:-1;
			break;
		case 2:
			$scope.showinfo.count2+=flag?1:-1;
			break;
		case 3:
			$scope.showinfo.count3+=flag?1:-1;
			break;
		case 4:
			$scope.showinfo.count4+=flag?1:-1;
			break;
		case 5:
			$scope.showinfo.count5+=flag?1:-1;
			break;
		case 6:
			$scope.showinfo.count6+=flag?1:-1;
			break;
		case 7:
			$scope.showinfo.count7+=flag?1:-1;
			break;
		case 8:
			$scope.showinfo.count8+=flag?1:-1;
			break;
		}
		console.log($scope.showinfo.count8);
	};
	if(localStorage["cotyTable_hides"]!=undefined){
		var cols =$.parseJSON(localStorage["cotyTable_hides"]);
		for(var i=0,j=cols.length;i<j;i++){
			$scope.setCols(cols[i],false);
		}
	}
	$scope.SearchData = function() {
		$scope.postData={};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		// 表单查询参数
		
		LoadList();
	};
	// 加载数据方法
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, $scope.selected,params);
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			if($scope.entity_name != null){
				params['entity_name'] = $scope.entity_name;
			}
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		// 加载数据
		DecorationSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.decorations = response.data.data;
			$scope.sumData = response.data.sumData;
			setTimeout(check_table_tr,500);
		});
	}
	
	
	// 配置分页基本参数
	$scope.$watch('pagination.refresh',LoadList);
	
	
	$scope.OpenUrl = function(c){
		location.href="#/decoration/detail/"+c.entity_code+"/"+c.decoration_no_str+"";
	};

	AppUtil.LoadGroups();
	//加载下拉列表数据
	$http.get("decoration/dicts").success(function(data){
		$scope.soStatusDescs=data.data.soStatusDescs;
		$scope.decTypeDescs=data.data.decTypeDescs;
		$scope.users=data.data.users;
	});
	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};
	
	// 双击跳转明细页面
	$scope.showDetail = function(entity_code,rebate_no) {
		$("#myModal_detail").modal("show");
		var postData = {};
		postData.entity_code = entity_code;
		postData.decoration_no = rebate_no;
		// 加载数据方法

		AppUtil.loading();
		$http.post('decoration/detail', postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.decoration_info = response.data.decoration_info;
			$scope.es_info = response.data.es_info;
			$scope.vehicle_info = response.data.vehicle_info;
			$scope.decoration_part_infos = response.data.decoration_part_infos;
		}).error(function() {
			alertMsg("提示", "系统出错,请稍后重试.");
			AppUtil.remove_loading();
		});

		$scope.formatStr = function(input){
			var returnStr = "";
			if(input == undefined){
				return;
			}
			var itemArr = input.split(";");
			for (var i = 0; i < itemArr.length; i++) {
				var temp = itemArr[i].split(":");
				if(temp.length>1){
					returnStr += temp[1];
				}else{
					returnStr += itemArr[i];
				}
			}
		    return returnStr;
		}

    };
});

app.register.service('DecorationSer', function($http,AppUtil) {
	this.list = function(postData) {
		AppUtil.loading();
		//设置查询开始下标
		postData.start= (postData.page-1<0?0:postData.page-1)*postData.size;
		return $http.post('decoration/list', postData).error(function() {
			AppUtil.remove_loading();
		});;
	};

});
